package com.cndw.kungfu.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.cndw.kungfu.comm.tencent.LogFieldType;
import com.cndw.kungfu.comm.tencent.QQDC;
import com.cndw.kungfu.config.Constants;
import com.cndw.kungfu.config.ConstantsCost;
import com.cndw.kungfu.config.ConstantsLevel;
import com.cndw.kungfu.config.ConstantsLog;
import com.cndw.kungfu.config.ConstantsRadio;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.ext.LangConfigLib;
import com.cndw.kungfu.ext.LanguageLib;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.DataStaticCenter;
import com.cndw.kungfu.model.PvpCommSerialize;
import com.cndw.kungfu.model.battle.Team;
import com.cndw.kungfu.model.pvp.api.CampApiClient;
import com.cndw.kungfu.model.pvp.camp.CampConfig;
import com.cndw.kungfu.model.pvp.camp.CampWar;
import com.cndw.kungfu.model.pvp.camp.CampWarPlayer;
import com.cndw.kungfu.model.pvp.campcross.CampTeam;
import com.cndw.kungfu.model.sys.LogServer;
import com.cndw.kungfu.vo.EventNotice;
import com.cndw.rpg.annotation.Controller;
import com.cndw.rpg.framework.ResponseMessage;
import com.cndw.rpg.framework.Session;

/**
 * 一些简单pvp接口
 * 
 * @author fantadust [9677889@qq.com]
 * @date 2012-8-5 下午5:08:30
 */
@Controller
public class WarPvpController {

	/**
	 * 阵营战显示接口
	 * 
	 * @param session
	 * @return
	 */
	public ResponseMessage campShow(Session session) {
		if (!CampWar.isOpen()) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.time"));
		}
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		if (player.getLevel() < CampConfig.applyLevel) {
			return null;
		}
		int imgLeft = CampWar.getNameIndexLeft();
		int imgRight = CampWar.getNameIndexRight();
		String nameLeft = LangConfigLib.getByIndex("config.camp.read", imgLeft);
		String nameRight = LangConfigLib.getByIndex("config.camp.blue", imgRight);
		boolean join = CampWar.isJoin(session.getUid());

		Collection<CampWarPlayer> joinsAll = CampWar.getAllWars();
		List<Object[]> allPlayers = new ArrayList<Object[]>();
		for (CampWarPlayer e : joinsAll) {
			allPlayers.add(e.toObj());
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("join", join);
		map.put("imgLeft", imgLeft);
		map.put("imgRight", imgRight);
		map.put("nameLeft", nameLeft);
		map.put("nameRight", nameRight);
		map.put("allPlayers", allPlayers);
		map.put("endTime", CampWar.getEndTime());
		int area = 0;
		if (join) {
			CampWarPlayer campWarPlayer = CampWar.getCampPlayer(session.getUid());
			campWarPlayer.setAct(true);
			player.getDataInfo().alterExpRet(Constants.achiveRet_warpvp);
			area = campWarPlayer.getArea();
			map.put("myInfo", campWarPlayer.toRecord());
			map.put("buff", campWarPlayer.getBuff());
			LogServer.recordLog(player, ConstantsLog.battle, 40402, true);
		} else {
			area = CampConfig.getArea(player.getLevel());
		}
		map.put("history", PvpCommSerialize.getInstance().getCampRecord(session.getUid()));
		map.put("records", CampWar.recordsView(area, 60));
		map.put("area", area);
		map.put("topPlayers", CampWar.getTop3());
		QQDC.getInstance().logClick(player, LogFieldType.camp);
		return ResponseMessage.getSuccMessage(map);
	}

	public ResponseMessage campList(Session session, int type) {
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		CampWarPlayer campWarPlayer = CampWar.getCampPlayer(session.getUid());
		int area = campWarPlayer == null ? CampConfig.getArea(player.getLevel()) : campWarPlayer.getArea();

		Map<String, Object> map = new HashMap<String, Object>();
		if (type == 0) {
			map.put("records", CampWar.recordsView(area, 60));
		} else {
			map.put("records", CampWar.recordsView(area, session.getUid(), 30));
		}
		map.put("area", area);

		return ResponseMessage.getSuccMessage(map);
	}

	/**
	 * 报名
	 * 
	 * @param session
	 * @param source
	 * @return
	 */
	public ResponseMessage campJoin(Session session) {
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		if (player.getLevel() < CampConfig.applyLevel) {
			return null;
		}
		if (!CampWar.isOpen()) {
			return null;
		}
		boolean addNew = !CampWar.isJoin(session.getUid());
		if (addNew) {// 已参加了
			CampWar.joinPlayer(player);
			player.getDataInfo().alterExpRet(Constants.achiveRet_warpvp);
		}
		CampWarPlayer camPlayer = CampWar.getCampPlayer(session.getUid());
		camPlayer.setAct(true);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("myInfo", camPlayer.toRecord());
		if (addNew) {
			EventNotice notice = new EventNotice(ConstantsRadio.camp, ConstantsRadio.campAdd);
			notice.comitData(camPlayer.toObj());
			notice.broadcast(CampWar.getAllIds());
		}
		LogServer.recordLog(player, ConstantsLog.battle, 40402, true);
		return ResponseMessage.getSuccMessage(map);
	}

	/**
	 * 退出比赛
	 * 
	 * @param session
	 * @return
	 */
	public ResponseMessage campExit(Session session) {
		if (!CampWar.isOpen()) {
			return null;
		}
		if (!CampWar.isJoin(session.getUid())) {// 已参加了
			return null;
		}
		CampWarPlayer camPlayer = CampWar.getCampPlayer(session.getUid());
		camPlayer.setAct(false);

		EventNotice notice = new EventNotice(ConstantsRadio.camp, ConstantsRadio.campExit);
		notice.comitData(camPlayer.toObj());
		notice.broadcast(CampWar.getAllIds());

		CampWar.quitPlayer(session.getUid());
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("suc", true);
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		LogServer.recordLog(player, ConstantsLog.battle, 40402, false);
		return ResponseMessage.getSuccMessage(map);
	}

	public ResponseMessage campClose(Session session) {
		CampWar.logoutUid(session.getUid());
		return null;
	}

	/**
	 * 鼓舞
	 * 
	 * @param session
	 * @return
	 */
	public ResponseMessage campBuff(Session session) {
		CampWarPlayer camPlayer = CampWar.getCampPlayer(session.getUid());
		if (camPlayer == null) {
			return null;
		}
		if (camPlayer.getBuff() >= 100) {
			return null;
		}
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		if (player.getGoldAll() < CampConfig.buffGold) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.gold"));
		}
		player.alterGoldAll(-CampConfig.buffGold);
		player.commitSync();

		camPlayer.setBuff(camPlayer.getBuff() + 5);

		LogServer.costEventLog(session.getUid(), CampConfig.buffGold, ConstantsCost.campBuff);
		LogServer.recordLog(player, ConstantsLog.battle, 40401, CampConfig.buffGold, camPlayer.getBuff());

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("suc", true);
		map.put("gold", player.getGold());
		map.put("goldBing", player.getGoldBing());
		map.put("buff", camPlayer.getBuff());
		return ResponseMessage.getSuccMessage(map);
	}

	public ResponseMessage crossCampFirst(Session session) {
		return CampApiClient.first(session.getUid());
	}

	public ResponseMessage crossCampJoin(Session session) {
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		Team team = player.getTeam();
		if (team == null || team.getMemberIds().size() != 3) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("warpvp.join.team"));
		}
		if (!team.isMaster(session.getUid())) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("team.err.master"));
		}
		for (long uid : team.getMemberIds()) {
			Player target = Context.getPlayerServer().getByPlayerId(uid);
			if (target.getLevel() < ConstantsLevel.lv70CampCross) {
				return ResponseMessage.ticpErrMsg(LanguageLib.get("warpvp.join.level", ConstantsLevel.lv70CampCross));
			}
		}
		LinkedList<Long> ids = new LinkedList<Long>(team.getMemberIds());
		ids.remove(session.getUid());
		ids.addFirst(session.getUid());

		CampTeam campTeam = new CampTeam(ids);

		Object msg = CampApiClient.join(campTeam);
		if (msg == null) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.sys"));
		}
		long m = Long.valueOf(msg.toString());
		if (m == -1) {
			team.setType(0);
			EventNotice noticeMembers = new EventNotice(ConstantsRadio.team, ConstantsRadio.teamCrossJoin);
			noticeMembers.put("join", true);
			noticeMembers.broadcast(team.getMemberIds());

			return ResponseMessage.getSuccMessage(true);
		} else if (m == -2) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("warpvp.join.time"));
		} else {
			Player target = Context.getPlayerServer().getByPlayerId(m);
			return ResponseMessage.ticpErrMsg(LanguageLib.get("warpvp.join.have", target.getPlayerName()));
		}
	}

	public ResponseMessage crossCampShow(Session session) {
		DataStaticCenter.campCrossOpen(session.getUid(), true);
		//LogUtil.getMain().debug("open " + session.getUid());
		return CampApiClient.show(session.getUid());
	}

	public ResponseMessage crossCampClose(Session session) {
		DataStaticCenter.campCrossOpen(session.getUid(), false);
		//LogUtil.getMain().debug("close " + session.getUid());
		return null;
	}

	public ResponseMessage crossCampLog(Session session) {
		return CampApiClient.log(session.getUid());
	}

	public ResponseMessage crossCampTeam(Session session, long masterId) {
		return CampApiClient.team(masterId);
	}

	public ResponseMessage crossCampTeamList(Session session) {
		return CampApiClient.teamlist();
	}

	public ResponseMessage crossCampOrder(Session session) {
		return CampApiClient.order();
	}

}
